<?php
namespace app\api\controller;

use think\Db;
use app\api\controller\Base;

class Notice extends Base
{
	//通知页面显示
	//我收到的
    public function index()
    {
        $uid=$this->uid;
        if(!empty($uid)){
            $where['f_message.uid']=$uid;
            $where['mflg']=1;
            $where['f_message.stoptime']=array('egt',date("Y-m-d H:i:s"));
			$uname=db('user')->where('uid',$uid)->value('uname');
			$nickname=db('user')->where('uid',$uid)->value('nickname');
			//对我感兴趣
			$st=db('message')->where($where)->field('id')->select();
          
          $res=db('message')
            ->join('praise','f_praise.mid=f_message.mid')
            ->join('user','f_praise.uid=f_user.uid')
            ->where($where)
            ->field('title,type,f_message.mid,picture,f_praise.createtime,mflg,f_praise.uid,uname,nickname,tflg')
            ->select();
          $url="https://www.mamingwang.cn/upload/message/";
          foreach ($res as $key=>$value)
          {
            if($uname!='')
            {
              $res[$key]['sname']=$uname;
            }else{
              $res[$key]['sname']=$nickname;
            }
            if($value['uname']!="")
            {
              $res[$key]['uname']=$value['uname'];
            }else{
              $res[$key]['uname']=$value['nickname'];
            }
            $res[$key]['state'] = 1;
            $a=$res[$key]['picture'];
            $arr=array_filter(explode(',',$a));
            if($arr==[]){
              $arr[0]="no.jpg";
            }
            $res[$key]['picture']=$url.$arr[0];
            //                     dump($res[$key]['picture']);die;
            $res[$key]['create_time']=$res[$key]['createtime'];
			$edit=db('praise')->where('tflg',1)->update(['tflg'=>0]);
          }

          //评论我的文章的
          $meRes=db('message')
            ->join('evalute','f_message.mid=f_evalute.message_id')
            ->join('user u','u.uid=f_message.uid')
            ->join('user s','f_evalute.pjid=s.uid')
            ->where($where)
            ->where('sfig',1)
            ->where('bpid',0)
            ->where('u.uflg',1)
            ->field('title,type,f_message.mid,picture,create_time,u.uname as uname,txt,s.uname as sname,s.uid,s.nickname,tflg')
            ->select();
          $url="https://www.mamingwang.cn/upload/message/";
          foreach($meRes as $key=>$value){
            if($uname!='')
            {
              $meRes[$key]['uname']=$uname;
            }else{
              $meRes[$key]['uname']=$nickname;
            }
            if($value['sname']!="")
            {
              $meRes[$key]['sname']=$value['sname'];
            }else{
              $meRes[$key]['sname']=$value['nickname'];
            }
            $a=$meRes[$key]['picture'];
            $arr=array_filter(explode(',',$a));        
            if($arr==[]){
              $arr[0]="no.jpg";
            }
            $meRes[$key]['picture']=$url.$arr[0];
            $meRes[$key]['state'] = 2;
          }
          //回复我评论的
          $evalute=db('evalute')
            ->join('message','f_evalute.message_id=f_message.mid')
            ->join('user u','f_evalute.pjid=f_user.uid')
            ->join('user s','f_evalute.bpid=s.uid')
            ->where('bpid',$uid)
            ->where('mflg',1)
            ->where('sfig',1)
            ->where('u.uflg',1)
            ->where('stoptime','egt',date("Y-m-d H:i:s"))
            ->field('title,type,f_message.mid,picture,create_time,u.uname as uname,txt,s.uname as sname,u.uid,u.nickname,tflg')
            ->select();
          $url="https://www.mamingwang.cn/upload/message/";
          foreach($evalute as $key=>$value){
            if($uname!='')
            {
              $evalute[$key]['sname']=$uname;
            }else{
              $evalute[$key]['sname']=$nickname;
            }
            if($value['uname']!="")
            {
              $evalute[$key]['uname']=$value['uname'];
            }else{
              $evalute[$key]['uname']=$value['nickname'];
            }
            $a=$evalute[$key]['picture'];
            $arr=array_filter(explode(',',$a));
            if($arr==[]){
              $arr[0]="no.jpg";
            }
            $evalute[$key]['picture']=$url.$arr[0];
            $evalute[$key]['state'] = 3;
			$edit=db('evalute')->where('tflg',1)->update(['tflg'=>0]);
          }
          //$result=['praise'=>$rs,'meRes'=>$meRes,'evalute'=>$evalute];
          $cards = array_merge($res, $evalute,$meRes);
          $ctime_str = array();
          foreach ($cards as $key => $v) {
            $ctime_str[] = $cards[$key]['create_time'];
          }
          array_multisort($ctime_str, SORT_DESC, $cards);
           if($cards){
				return $this->AjaxReturn("1","操作成功",$cards);
		   }else{
				return $this->AjaxReturn("-1","暂无评论和感兴趣");
		   }
		}
		else{
			return $this->AjaxReturn("-1","暂无信息");
		}
    }

	public function numCount()
    {
        $uid=$this->uid;
        if(!empty($uid)){
            $where['f_message.uid']=$uid;
            $where['mflg']=1;
			$where['tflg']=1;
            $where['f_message.stoptime']=array('egt',date("Y-m-d H:i:s"));
            
			$whereData['sfig']=1;
			$whereData['bpid']=0;
			$whereData['u.uflg']=1;

			$whereEvaData['bpid']=$uid;
			$whereEvaData['mflg']=1;
			$whereEvaData['sfig']=1;
			$whereEvaData['u.uflg']=1;
            $whereEvaData['tflg']=1;
			$whereEvaData['f_message.stoptime']=array('egt',date("Y-m-d H:i:s"));
			
			//给我点赞的条数
			$resCount=db('message')
				->join('praise','f_praise.mid=f_message.mid')
				->join('user','f_praise.uid=f_user.uid')
				->where($where)
				->count();
          
            //评论我的文章的条数
            $meResCount=db('message')
				->join('evalute','f_message.mid=f_evalute.message_id')
				->join('user u','u.uid=f_message.uid')
				->join('user s','f_evalute.pjid=s.uid')
				->where($where)
				->where($whereData)
				->count();
          
            //回复我评论的条数
            $evaluteCount=db('evalute')
				->join('message','f_evalute.message_id=f_message.mid')
				->join('user u','f_evalute.pjid=f_user.uid')
				->join('user s','f_evalute.bpid=s.uid')
				->where($whereEvaData)
				->count();

		   $a=array($resCount,$meResCount,$evaluteCount);
		   //array_sum($a);
           if($a){
				return $this->AjaxReturn("1", [array_sum($a)]);
		   }else{
				return $this->AjaxReturn("-1");
		   }
		}
		else{
			return $this->AjaxReturn("-1");
		}
    }
    
	//我发出的
	public function send()
    {
        $uid=$this->uid;
        if(!empty($uid)){
            $where['f_praise.uid']=$uid;
            $where['mflg']=1;
            $where['f_message.stoptime']=array('egt',date("Y-m-d H:i:s"));
			$uname=db('user')->where('uid',$uid)->value('uname');
			$nickname=db('user')->where('uid',$uid)->value('nickname');
          
          //我感兴趣的
          $res=db('praise')
            ->join('message','f_praise.mid=f_message.mid')
            ->join('user','f_message.uid=f_user.uid')
            ->where($where)
            ->field('title,type,f_message.mid,picture,f_praise.createtime,mflg,gflg,uname as sname,f_message.uid,nickname')
            ->select();
          $url="https://www.mamingwang.cn/upload/message/";
          foreach ($res as $key=>$value)
          {
            if($uname)
            {
              $res[$key]['uname']=$uname;
            }else{
              $res[$key]['uname']=$nickname;
            }
            if($value['gflg']=="1"){
               $res[$key]['sname']='农厂汇官网';
            }else{
              if($value['sname']!="")
              {
                $res[$key]['sname']=$value['sname'];
              }else{
                $res[$key]['sname']=$value['nickname'];
              }
            }
            $res[$key]['state'] = 1;
            $a=$res[$key]['picture'];
            $arr=array_filter(explode(',',$a));
            if($arr==[]){
              $arr[0]="no.jpg";
            }
            $res[$key]['picture']=$url.$arr[0];
          }
          //我评论的
          $evalute=db('evalute')
            ->join('message','f_evalute.message_id=f_message.mid')
            ->join('user','f_user.uid=f_message.uid')
            ->where('pjid',$uid)
            ->where('mflg',1)
            ->where('sfig',1)
            ->where('stoptime','egt',date("Y-m-d H:i:s"))
            ->field('title,type,f_message.mid,picture,create_time,txt,uname,uflg,gflg,f_message.uid,nickname')
            ->select();
          $url="https://www.mamingwang.cn/upload/message/";
          foreach ($evalute as $key=>$value)
          {   
            if($uname)
            {
              $evalute[$key]['sname']=$uname;
            }else{
              $evalute[$key]['sname']=$nickname;
            }
            if($value['gflg']=="1")
            {
              $evalute[$key]['uname']='农厂汇官方';
            }else{
              if($value['uname']!="")
              {
                $evalute[$key]['uname']=$value['uname'];
              }else{
                $evalute[$key]['uname']=$value['nickname'];
              }
            }
            $evalute[$key]['state'] = 2;
            $evalute[$key]['createtime'] = $evalute[$key]['create_time'];
            $a=$evalute[$key]['picture'];
            $arr=array_filter(explode(',',$a));
            if($arr==[]){
              $arr[0]="no.jpg";
            }
            $evalute[$key]['picture']=$url.$arr[0];
          }
          //$result=['praise'=>$res,'evalute'=>$evalute];
          $cards = array_merge($res, $evalute); 
          $ctime_str = array();
          foreach ($cards as $key => $v) {
            $ctime_str[] = $cards[$key]['createtime'];
          }
          array_multisort($ctime_str, SORT_DESC, $cards);
          //shuffle($cards);随机排列 //dump($cards);
            if($cards){
				 return $this->AjaxReturn("1","操作成功",$cards);
			}
			else
			{
				return $this->AjaxReturn("-1","暂无评论和感兴趣");
			}

        }else{
            return $this->AjaxReturn("-1","暂无信息");
        }
    }
  
}
